package edu.jkd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.jkd.dto.OrderDto;
import edu.jkd.entity.Customer;
import edu.jkd.entity.Myorder;
import edu.jkd.entity.Product;
import edu.jkd.service.ICustomerService;
import edu.jkd.service.IMyorderService;
import edu.jkd.service.IProductService;
import edu.jkd.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping("order")
public class MyOrderController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IMyorderService myorderService;

    @Autowired
    private IProductService productService;
    /**
     * 新增订单
     */
    @GetMapping("/{proId}/{proPrice}")
    public ModelAndView save(@PathVariable("proId") Integer proId,
                             @PathVariable("proPrice") BigDecimal proPrice,HttpServletRequest request){
        Myorder order = new Myorder();
        order.setOrderNumber(UUID.randomUUID().toString().replace("-",""));
        Customer cust =(Customer) request.getSession().getAttribute("user");
        order.setCustId(cust.getCustId());
        order.setProId(proId);
        order.setOrderPrice(proPrice);
        order.setOrderCreateTime(LocalDateTime.now());
        order.setStatus(1);
        //新增一条记录到DB
        myorderService.save(order);
        Product product = productService.getById(order.getProId());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("order",order);
        modelAndView.addObject("product",product);
        modelAndView.setViewName("confirm");
        return modelAndView;
    }

    /**
     * 查看所有订单
     * @return
     */
    @GetMapping()
    public ModelAndView getAll(HttpServletRequest request){
        LambdaQueryWrapper<Customer> queryWrapper=new LambdaQueryWrapper<>();
        Customer c1=(Customer) request.getSession().getAttribute("user");
        queryWrapper.eq(Customer::getCustName,c1.getCustName());
        Customer one = customerService.getOne(queryWrapper);
        LambdaQueryWrapper<Myorder> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Myorder::getCustId,one.getCustId());
        List<Myorder> myorderList = myorderService.list(queryWrapper1);
        List<OrderDto> orderDtoList=new ArrayList<>();


        for (Myorder item: myorderList) {
            OrderDto orderDto = new OrderDto();
            Product product = productService.getById(item.getProId());
            orderDto.setMyorder(item);
            orderDto.setProName(product.getProName());
            orderDto.setProDesc(product.getProDesc());
            orderDtoList.add(orderDto);
        }
        ModelAndView mav=new ModelAndView("order_list");
        mav.addObject("orderDtoList",orderDtoList);
        //
        int x=orderDtoList.size()/5;
        if (orderDtoList.size()%5!=0){
            x+=1;
        }
        request.getSession().setAttribute("total",x);
        return mav;
    }
    /**
     * 删除订单
     * */
    @RequestMapping("/deleteOrderByIds")
    @ResponseBody
    public Object deleteOrderByIds(String [] id){

      return myorderService.deleteOrderById(id);
    }

    /**
     *
     * @param currentPage 当当前页
     * @param pageSize 显示条数
     * @param a 标识符，区分路径
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}/{a}")
    public ModelAndView selectByPage(@PathVariable("currentPage")Integer currentPage,
                               @PathVariable("pageSize")Integer pageSize,@PathVariable("a") int a,HttpSession session){
        ModelAndView mov = new ModelAndView();

        Customer user = (Customer)session.getAttribute("user");
        List<Myorder> myorderList = myorderService.selectByPage(currentPage, pageSize,user.getCustId());

        List<OrderDto> orderDtoList=new ArrayList<>();
        for (Myorder item: myorderList) {
            OrderDto orderDto = new OrderDto();
            Product product = productService.getById(item.getProId());
            orderDto.setMyorder(item);
            orderDto.setProName(product.getProName());
            orderDto.setProDesc(product.getProDesc());
            orderDtoList.add(orderDto);
        }
//
        mov.addObject("orderDtoList",orderDtoList);
        mov.setViewName("order_list");
        return  mov;
    }




}
